package com.lwbldy.modules.system.service.impl;


import com.lwbldy.common.shiro.ShiroUtils;
import com.lwbldy.common.tools.StringUtils;
import com.lwbldy.modules.system.domain.SysUser;
import com.lwbldy.modules.system.domain.SysUserRole;
import com.lwbldy.modules.system.mapper.SysRoleDao;
import com.lwbldy.modules.system.mapper.SysUserDao;
import com.lwbldy.modules.system.mapper.SysUserRoleDao;
import com.lwbldy.modules.system.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;


@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysUserRoleDao sysUserRoleDao;

	@Override
	public SysUser queryById(Long userId) {
		SysUser sysUser = sysUserDao.queryObject(userId);
		sysUser.setRoleIdList(sysUserRoleDao.queryRoleIdList(userId));
		return sysUser;
	}

	@Override
	public SysUser queryByName(String userName) {
		return sysUserDao.queryByName(userName);
	}

	@Override
	public List<SysUser> queryList(Map<String, Object> map) {
		return sysUserDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return sysUserDao.queryTotalByMap(map);
	}

	@Override
	@Transactional
	public int save(SysUser sysUser){
		sysUser.setCreateTime(new Date());
		sysUser.setStatus(1);
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		sysUser.setSalt(salt);
		sysUser.setPassword(ShiroUtils.sha256(sysUser.getPassword(), sysUser.getSalt()));
		int r = sysUserDao.save(sysUser);
		if(r != 1){
			return 0;
		}
		sysUserRoleDao.deleteByUserId(sysUser.getUserId());
		updateSysUserRole(sysUser.getRoleIds(),sysUser.getUserId());
		return r;
	}

	@Override
	public int save(List<SysUser> list) {
		return sysUserDao.saveBatch(list);
	}

	@Override
	public int update(SysUser sysUser) {
		return sysUserDao.update(sysUser);
	}

	@Override
	public int updateUserAndRole(SysUser sysUser){
		sysUserRoleDao.deleteByUserId(sysUser.getUserId());
		updateSysUserRole(sysUser.getRoleIds(),sysUser.getUserId());
		return sysUserDao.update(sysUser);
	}

	/**
	 * 更新角色和权限
	 * @param roleIds
	 * @param userId
	 */
	private void updateSysUserRole(String roleIds,Long userId){
		List<Long> ids = StringUtils.StringToLongArr(roleIds.split(","));
		List<SysUserRole> roles = new ArrayList<>();
		//保存用户角色
		if(ids.size() > 0){
			for(Long id : ids){
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setRoleId(id);
				sysUserRole.setUserId(userId);
				roles.add(sysUserRole);
			}
			sysUserRoleDao.saveBatch(roles);
		}
	}

	@Override
	public int remove(Long userId) {
		return sysUserDao.delete(userId);
	}

	@Override
	public int removeBatch(Long[] ids) {
		return sysUserDao.deleteBatch(ids);
	}

	@Override
	public List<String> queryAllPerms(Long userId) {
		return sysUserDao.queryAllPerms(userId);
	}

	@Override
	public int changePWD(Long userId, String pwd) {
		SysUser sysUser = new SysUser();
		sysUser.setUserId(userId);
		String salt = RandomStringUtils.randomAlphanumeric(20);
		sysUser.setSalt(salt);
		sysUser.setPassword(pwd);
		sysUser.setPassword(ShiroUtils.sha256(sysUser.getPassword(), sysUser.getSalt()));
		int r = sysUserDao.update(sysUser);
		return r;
	}
}
